<?php
/**
 * 批量转换淘宝客数字商品Id为新商品ID B段
 */
ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki;
use Biz\tools\TbkUnion;

$page = $argv[1] ?? 1;
$allCount = Wiki::count(['channel' => Wiki::CHANNEL_TAOBAO, 'channel_jump_params' => '']);
var_dump('all count:' . $allCount);

$week = date('w');
$hour = date('H');
if (!isset($argv[1]) && in_array($week, [3, 5, 0]) && $hour < 12) {
    $allPage = ceil($allCount / 10000);
    var_dump('all page:' . $allPage);
    for ($i = 1; $i <= $allPage; $i++) {
        trans($i);
    }
} else {
    trans($page);
}

function trans($page)
{
    $pageSize = 10000;
    $res = Wiki::select(['id', 'title', 'channel', 'channel_spu_id', 'channel_jump_params'], [
            'channel' => Wiki::CHANNEL_TAOBAO, 'channel_jump_params' => '',
            'LIMIT' => [($page - 1) * $pageSize, $pageSize],
            'ORDER' => ['id' => 'DESC']
        ]
    );
    $wikiCount = count($res);
    var_dump('page:' . $page . '#wiki count:' . $wikiCount);
    $size = 30;
    $i = 1;
    do {
        $list = array_slice($res, ($i - 1) * $size, $size);
        echo $i;
        getTrans($list);
        $i++;
    } while ($list);
}

function getTrans($list, $retry = 0)
{
    $channelSpuIds = array_column($list, 'channel_spu_id');
    /*$isTbkList = Biz\cache\wiki\TbkGoods::isTbkGoods($channelSpuIds);
    $itemIds = [];
    foreach ($channelSpuIds as $spuId) {
        if ($isTbkList[$spuId]) {
            $itemIds[] = $spuId;
        }
    }*/
    $itemIds = $channelSpuIds;
    $transResult = TbkUnion::tbkItemIdtransForm($itemIds);
    $getTransResCount = count($transResult);
    echo $retry ? ('####restry:' . $retry) : '';
    echo '#get trans count:' . $getTransResCount;
    echo PHP_EOL;
    foreach ($list as $row) {
        $itemId = $row['channel_spu_id'];
        if (isset($transResult[$itemId]) && $transResult[$itemId]) {
            $data = [
                'channel_jump_params' => $transResult[$itemId]
            ];
            $couponInfo = TbkUnion::tbkCouponConvert($transResult[$itemId], TbkUnion::ADZONE_ID, '', TbkUnion::BIZ_SCENE_ID3);
            if ($couponInfo && $couponInfo['item_url']) {
                \Biz\cache\wiki\TbkGoods::addGoods([$itemId]);
                \Biz\cache\wiki\TbkUrl::setUrl($itemId, $couponInfo['item_url']);
            }
            $itemInfo = TbkUnion::getItemInfo($transResult[$itemId], TbkUnion::BIZ_SCENE_ID3);
            $optionalInfo = TbkUnion::tbkDgOpMaterialOptional($itemInfo['item_url']);
            $optionalInfo = $optionalInfo[0] ?? [];
            if ($optionalInfo) {
                if ($optionalInfo['white_image']) {
                    $data['white_image'] = $optionalInfo['white_image'];//白底图
                }
                if ($optionalInfo['short_title']) {
                    $data['simple_title'] = $optionalInfo['short_title'];//商品短标题
                }
            }
            Wiki::update($data, ['id' => $row['id']]);
        }
    }
    if ($getTransResCount == 0 && $retry < 2 && !empty($itemIds)) {
        $retry = $retry + 1;
        getTrans($list, $retry);
    }
}
